package it.cums.ui.actions;

import it.cums.ui.Activator;
import it.cums.ui.localization.Messages;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;

public class NewAction implements IWorkbenchWindowPulldownDelegate {
	private LocalResourceManager resources = new LocalResourceManager(JFaceResources.getResources());

	  /*
	   * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
	   */
	  public Menu getMenu(Control parent) {
	    Menu menu = new Menu(parent);

	    MenuItem newOrder = new MenuItem(menu, SWT.PUSH);
	    newOrder.setText(Messages.NewAction_0);
	    newOrder.setImage(resources.createImage(Activator.getImageDescriptor("icons/order-new.gif"))); //$NON-NLS-1$
	    newOrder.addSelectionListener(new SelectionAdapter() {
	      
	      public void widgetSelected(SelectionEvent e) {
	    	  try {
	    		  addOrder();
	    	  } catch (Exception e1) {
	    		  Activator.getDefault().logError(e1.getMessage(), e1);
	    	  }
	      }
	    });

	    MenuItem newCustomer = new MenuItem(menu, SWT.PUSH);
	    newCustomer.setText(Messages.NewAction_2);
	    newCustomer.setImage(resources.createImage(Activator.getImageDescriptor("icons/person-new.gif"))); //$NON-NLS-1$
	    newCustomer.addSelectionListener(new SelectionAdapter() {
	      
	      public void widgetSelected(SelectionEvent e) {
	    	  try {
	    		  addCustomer();
	    	  } catch (Exception e1) {
	    		  Activator.getDefault().logError(e1.getMessage(), e1);
	    	  }
	      }
	    });

	    MenuItem newProduct = new MenuItem(menu, SWT.PUSH);
	    newProduct.setText(Messages.NewAction_4);
	    newProduct.setImage(resources.createImage(Activator.getImageDescriptor("icons/product-new.gif"))); //$NON-NLS-1$
	    newProduct.addSelectionListener(new SelectionAdapter() {
	      
	      public void widgetSelected(SelectionEvent e) {
	    	  try {
	    		  addProduct();
	    	  } catch (Exception e1) {
	    		  Activator.getDefault().logError(e1.getMessage(), e1);
	    	  }
	      }
	    });

	    MenuItem newSupplier = new MenuItem(menu, SWT.PUSH);
	    newSupplier.setText(Messages.NewAction_6);
	    newSupplier.setImage(resources.createImage(Activator.getImageDescriptor("icons/person-new.gif"))); //$NON-NLS-1$
	    newSupplier.addSelectionListener(new SelectionAdapter() {
	      
	      public void widgetSelected(SelectionEvent e) {
	    	  try {
	    		  addSupplier();
	    	  } catch (Exception e1) {
	    		  Activator.getDefault().logError(e1.getMessage(), e1);
	    	  }
	      }
	    });

	    return menu;
	  }

	  /*
	   * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
	   */
	  public void init(IWorkbenchWindow window) {
		  
	  }

	  /*
	   * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	   */
	  public void run(IAction action) {
		  addOrder();
	  }

	  private void addOrder() {
		  new NewOrderAction().run(null);
	  }

	  private void addCustomer() {
		  new NewCustomerAction().run(null);
	  }

	  private void addSupplier() {
		  new NewSupplierAction().run(null);
	  }

	  private void addProduct() {
		  new NewProductAction().run(null);
	  }

	public void dispose() {
		resources.dispose();
	}

	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		
	}
}
